import serial
import time
import string
import pynmea2
from flask import Flask,jsonify

def ding():
    start_time = time.time()
    while True:
        if time.time()-start_time>=1:
            break
        ser=serial.Serial('/dev/ttyAMA0', baudrate=9600, timeout=0.5)
        dataout = pynmea2.NMEAStreamReader()
        newdata=ser.readline()

        if newdata[0:6] == "$GPRMC":
            newmsg=pynmea2.parse(newdata)
            lat=str(newmsg.latitude)
            lng=str(newmsg.longitude)
            
            gps = "Latitude=" + lat + "and Longitude=" + lng
            print(gps)
            
            
        else:
            lat = '118.316937'
            lng = '24.617860'
            
    return lat,lng


app = Flask(__name__)

@app.route('/')
def index():
    lat,lng = ding()
    
    data = {
        'lat':lat,
        'lng':lng
    }
    return jsonify(data)

if __name__ == '__main__':
    app.run(host='192.168.43.117',port=5010,debug=True)
    
